var editindex=null;
function saverow(index){
    $('#grid_div').datagrid("endEdit", index);
    var rows = $('#grid_div').datagrid('getRows');
    var row = rows[index];
    var kcId = row.kcId;
    if(kcId!=null){
        editRow(row)
    }else{
        addRow(row)
    }
};
// function addRow(row){
//     $.ajax({
//         url: "user/doAdd.do",
//         type: "POST",
//         data:{
//             kcUsername:row.kcUsername,
//             kcPassword:row.kcPassword,
//             kcName:row.kcName,
//             kcSex:row.kcSex,
//             userlevel:row.userlevel,
//             kcAddress:row.kcAddress,
//             phone:row.phone
//         },
//         dataType : "json",
//         success : function(result) {
//             if(result.flg){
//                 row.kcId=result.kcId;
//                 alert(result.result);
//
//             }else{
//                 alert(result.result);
//             }
//         }
//     });
//
//    alert(row.kcUsername);
//
// };
function editRow(row){
    $.ajax({
        type:'post',
        url: "user/doEdit.do",
        data:{
            kcId:row.kcId,
            kcUsername:row.kcUsername,
            kcPassword:row.kcPassword,
            kcName:row.kcName,
            kcSex:row.kcSex,
            userlevel:row.userlevel,
            kcAddress:row.kcAddress,
            phone:row.phone
        },
        dataType : "json",
        success : function(result) {
            if(result.flg){
                alert(result.result);
            }else{
                alert(result.result);
            }
        },
        error:function(resuledata){
            alert(resuledata)
            if(resuledata.flg){
                alert(resuledata.result);
            }else{
                alert(resuledata.result);
            }
        }
    });

};

function deletedRow(index){
    $('#grid_div').datagrid("endEdit", index);
    var rows = $('#grid_div').datagrid('getRows');
    var row = rows[index];
    var kcId = row.kcId;
    alert(kcId);
    if(kcId==null){
        $('#grid_div').datagrid('deleteRow',index);
    }else{
        $.ajax({
            url: "user/deleteUser.do",
            data:{
                deleteId:kcId
            },
            success: function(){
                $('#grid_div').datagrid('deleteRow',index);
                alert('删除成功'+kcId);
            }});
    }

};
$(function () {
    $('#testType').combobox({
        url:"/type/getType",
        editable:false,
        valueField:'id',
        textField:'text',
        panelWidth:130,
        width:130,
        onLoadSuccess:function(data){
            $('#testType').combobox('setValue',data[0].id);
            },
        onSelect:function(record){
            getcolumns(record.id);
        }
    });

})
function getcolumns(testType){
    var array =[];
    var columns=[];
    if(testType==null||testType.length==0){
        testType="1";
    }
    $.ajax({
        url: 'type/getById?id='+testType,
        type:'POST',
        success:function(data){//data后台的传过来的表字段数组
            $(data).each(function(){
                array.push({field:'',title:'',width:''});
            });
            columns.push(array);
            for(var i=0;i<data.length;i++){
                columns[0][i]['field']= data[i];
                columns[0][i]['title']= data[i];
                columns[0][i]['width']= "130";
                }
            $('#grid_div').datagrid({
                columns:columns
            });
            for(var i=0;i<data.length;i++) {
                var tt = $('#grid_div').datagrid("getColumnOption", data[i]);
                tt.editor={type:'text'};
            }
        },
        dataType:'json'
    });
};
function send(){
    if(editindex!=null){
        $('#grid_div').datagrid("endEdit", editindex);
        // var rows =  $('#grid_div').datagrid('getAllRows');
        var rows = $('#grid_div').datagrid('getData').rows;
        for(var i=0;i<rows.length;i++){
            var row=rows[i];
            alert(JSON.stringify(row));
            for(var o in row){
                alert(row[o]);
            }
//             for(var j=0;j<row.length;j++){
//                 alert(row[j]);
//             }
        }
        var typeurl = $('#testurl').val();
        var testtime = $('#testtime').val();
        var params = {
            typeurl:typeurl,
            testtime:testtime,
            sendList:JSON.stringify(rows)
        };
        $.ajax({
            url: '/type/send',
            data : params,
            type:'POST',
            success:function(data){//data后台的传过来的表字段数组
               alert("成功");
            },
            dataType:'json'
        });
    }else{
        alert("请输入数据");
    }
}

jQuery(function(){
    getcolumns();
    var toolbarArray = [
        {id:"add",text:"添加数据",iconCls:"icon-add",handler:function(){
            $('#grid_div').datagrid('appendRow',{
                }
            );
        }},
        {id:"add",text:"重新刷新",iconCls:"icon-reload",handler:function(){
            jQuery("#grid_div").datagrid('reload');
        }}
    ];
    jQuery("#grid_div").datagrid({
        region:						"center",
        title:						"数据列表",
        // url:						"type/getById",
        loadMsg:					"正在加载数据，请稍等",
        rownumbers:					true,
        striped:					true,
        fit:						true,
        pagination:					true,
        toolbar:					toolbarArray,
        onClickRow: function(index,field,value){

        	if(editindex==null){
        		$(this).datagrid('beginEdit', index);
                editindex=index;
            }else{
//                $('#grid_div').datagrid("cancelEdit", editindex);
            	$('#grid_div').datagrid("endEdit", editindex);
                $(this).datagrid('beginEdit', index);
                editindex=index;
            }
        }
    });
})
